﻿Imports DevComponents.DotNetBar.SuperGrid

Public Class PRODUCTCAT
    Dim connect As CONDB = CONDB.NewConnection()
    Public Shared PCKPID As String
    Dim PROVIDER_Check As New PROVIDERSCLASS

    Private Sub PCKPIDTxt_TextChanged(sender As Object, e As EventArgs) Handles PCKPIDTxt.TextChanged
        If PCKPIDTxt.Text.ToString.Length > 0 Then
            PCKPID = PCKPIDTxt.Text
        Else
            PCKPID = 0
        End If
    End Sub

    Public Sub New()
        InitializeComponent()
        ' Initialize the grid and set the sample description text

        InitializeGrid()
    End Sub

    Private Sub InitializeGrid()
        Dim panel As GridPanel = masproductGrid.PrimaryGrid
        Dim column As GridColumn = panel.Columns("นำเข้ารายการ")
        '    Dim form As productCategorie 
        'แก้ parameter ใน panel.column ให้เป็นชื่อคอลัมน์ที่เป็นปุ่ม
        column.EditorType = GetType(btnReq)

        Dim panel2 As GridPanel = pckListProductGrid.PrimaryGrid
        Dim column2 As GridColumn = panel2.Columns("นำออกจากรายการ")
        column2.EditorType = GetType(btnReq2)
    End Sub

    Public Class btnReq
        Inherits GridButtonXEditControl

        Public Sub New()
            AddHandler Click, AddressOf btnReqClick
        End Sub

        Private Sub btnReqClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
            Dim s As String = TryCast(EditorCell.Value, String)
            Dim cell As GridCell = EditorCell
            '------Do something here when click button
            If PCKPID <> 0 And cell.GridRow.Cells("รหัสผลิตภัณฑ์").Value.ToString.Length > 0 Then
                Dim connect As CONDB = CONDB.NewConnection()

                Dim objInsertData As New productCategorieClass
                objInsertData.PCKPID_ = PCKPID
                objInsertData.PRDCODE_ = cell.GridRow.Cells("รหัสผลิตภัณฑ์").Value
                objInsertData.insertPcklistproduct()
                PRODUCTCAT.refreshPcklistpro()
                Me.Refresh()
            Else
                MsgBox("ท่านต้องทำการเลือกหมวดสินค้าก่อน")
            End If
        End Sub
    End Class

    Public Class btnReq2
        Inherits GridButtonXEditControl

        Public Sub New()
            AddHandler Click, AddressOf btnReqClick
        End Sub

        Private Sub btnReqClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
            Dim s As String = TryCast(EditorCell.Value, String)
            Dim cell As GridCell = EditorCell
            '------Do something here when click button
            Dim connect As CONDB = CONDB.NewConnection()

            Dim delStat As Integer
            delStat = MsgBox("ท่านต้องการลบผลิตภัณฑ์ดังกล่าวออกจากหมวดใช่หรือไม่", MsgBoxStyle.OkCancel)

            If delStat = 1 Then
                If cell.GridRow.Cells("รหัสสินค้าในกลุ่มผลิตภัณฑ์ย่อย").Value <> 0 Then
                    Dim objDeleteData As New productCategorieClass
                    objDeleteData.PCKLSTID_ = cell.GridRow.Cells("รหัสสินค้าในกลุ่มผลิตภัณฑ์ย่อย").Value
                    objDeleteData.delPcklistproduct()
                    PRODUCTCAT.refreshPcklistpro()
                    Me.Refresh()
                End If
            End If
        End Sub
    End Class

    Private Sub refreshPcklistpro()
        Dim objSelectProductCategorie As New productCategorieClass
        Dim pckListData As DataTable
        pckListData = connect.GetTable(objSelectProductCategorie.selectPcklistproduct_)
        'masgrpdrgData = connectlocal.GetTable(objSelectDataMasgrpdrg.selectMasgrpdrg_)
        pckListProductGrid.PrimaryGrid.DataSource = pckListData
    End Sub

    Private Sub ButtonX1_Click(sender As Object, e As EventArgs) Handles ButtonX1.Click
        Dim objSelectProductlistFromCategorie As New productCategorieClass
        Dim pckProductlistData = objSelectProductlistFromCategorie.queryProductlistFromCategorie(PCKPIDTxt.Text)
        pckListProductGrid.PrimaryGrid.DataSource = pckProductlistData
    End Sub

    Private Sub PRODUCTCAT_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim objSelectProductCategorie As New productCategorieClass
        'Dim s As FILTERCLASS
        'Dim s2 As FILTERCLASS
        ''------------หนูน้อยอะไรหว่า
        'Dim sql As String = "SELECT PCKPID, PCKNAME, REMARK FROM pckproduct WHERE STATUS = '1'"
        's = New FILTERCLASS(pckpInProductipriceTxt, sql, "รหัสกลุ่มผลิตภัณฑ์,ชื่อกลุ่มผลิตภัณฑ์,หมายเหตุ", "50,160,160", "1,1,1", "1,1,1")
        ''s.SetShowInDown = False
        's.SetShowBorder = True

        'Dim sql2 As String = "SELECT a.`PRDCODE`, a.`PRDNAME`, b.`CATNAME` FROM masproduct a JOIN masprdcats b ON a.PRDCAT = b.PRDCAT WHERE STATUS = '1'"
        's2 = New FILTERCLASS(productlistInPrdpriceTxt, sql2, "รหัสผลิตภัณฑ์,ชื่อผลิตภัณฑ์,กลุ่มผลิตภัณฑ์", "50,160,160", "1,1,1", "1,1,1")
        ''s.SetShowInDown = False
        's2.SetShowBorder = True
        ''------------end of หนูน้อยอะไรหว่า

        Dim pckProductData As DataTable
        pckProductData = connect.GetTable(objSelectProductCategorie.selectPckproduct_)
        'masgrpdrgData = connectlocal.GetTable(objSelectDataMasgrpdrg.selectMasgrpdrg_)
        pckProductGrid.PrimaryGrid.DataSource = pckProductData

        Dim productData As DataTable
        productData = connect.GetTable(objSelectProductCategorie.selectMasproduct_)
        'masgrpdrgData = connectlocal.GetTable(objSelectDataMasgrpdrg.selectMasgrpdrg_)
        masproductGrid.PrimaryGrid.DataSource = productData

        DesignForm(Me)
        DesignGrid(pckProductGrid)
        DesignGrid(masproductGrid)
        DesignGrid(pckListProductGrid)

        PROVIDER_Check.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub


    Private Sub pckProductGrid_CellClick(sender As Object, e As GridCellClickEventArgs) Handles pckProductGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        'f.TXT_CID.Text = grid.Cells.Item(2).Value
        PCKPIDTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสกลุ่มผลิตภัณฑ์").Value()
        PCKNAMETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อกลุ่มผลิตภัณฑ์").Value()

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("หมายเหตุ").Value().ToString.Length > 0 Then
            REMARKTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("หมายเหตุ").Value()
        Else
            REMARKTxt.Text = ""
        End If

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value() = True Then
            pckproductStatusChk.Checked = True
        Else
            pckproductStatusChk.Checked = False
        End If

        Dim objChkboxUpdate As New CLICKGRIDCHECKBOX
        objChkboxUpdate.clickChkboxUpdate("pckproduct", CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value(), "STATUS", "PCKPID", PCKPIDTxt.Text)

        Dim objSelectProductlistFromCategorie As New productCategorieClass
        Dim pckProductlistData = objSelectProductlistFromCategorie.queryProductlistFromCategorie(PCKPIDTxt.Text)
        pckListProductGrid.PrimaryGrid.DataSource = pckProductlistData
    End Sub

    Private Sub addPckproduct_Click(sender As Object, e As EventArgs) Handles addPckproduct.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PADD_ = True Then
                If PCKNAMETxt.Text.ToString.Length > 0 Then
                    Dim objInsertData As New productCategorieClass
                    objInsertData.PCKNAME_ = PCKNAMETxt.Text
                    objInsertData.REMARK_ = REMARKTxt.Text
                    objInsertData.STATUS_ = pckproductStatusChk.Checked
                    objInsertData.insertPckproduct()

                    Dim objSelectProductCategorie As New productCategorieClass
                    Dim pckProductData As DataTable
                    pckProductData = connect.GetTable(objSelectProductCategorie.selectPckproduct_)
                    'masgrpdrgData = connectlocal.GetTable(objSelectDataMasgrpdrg.selectMasgrpdrg_)
                    pckProductGrid.PrimaryGrid.DataSource = pckProductData

                    PCKPIDTxt.Text = ""
                    PCKNAMETxt.Text = ""
                    REMARKTxt.Text = ""
                    pckproductStatusChk.Checked = False
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน")
                End If
            Else
                MsgBox(PROVIDER_Check.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub editPckproduct_Click(sender As Object, e As EventArgs) Handles editPckproduct.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PUPD_ = True Then
                If PCKPIDTxt.Text.ToString.Length > 0 And PCKNAMETxt.Text.ToString.Length > 0 And REMARKTxt.Text.ToString.Length > 0 Then
                    Dim editStat As Integer
                    editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลหมวดสินค้าดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)

                    If editStat = 1 Then
                        Dim objEditData As New productCategorieClass
                        objEditData.PCKPID_ = PCKPIDTxt.Text
                        objEditData.PCKNAME_ = PCKNAMETxt.Text
                        objEditData.REMARK_ = REMARKTxt.Text
                        objEditData.STATUS_ = pckproductStatusChk.Checked
                        objEditData.editPckproduct()

                        Dim objSelectProductCategorie As New productCategorieClass
                        Dim pckProductData As DataTable
                        pckProductData = connect.GetTable(objSelectProductCategorie.selectPckproduct_)
                        'masgrpdrgData = connectlocal.GetTable(objSelectDataMasgrpdrg.selectMasgrpdrg_)
                        pckProductGrid.PrimaryGrid.DataSource = pckProductData

                        PCKPIDTxt.Text = ""
                        PCKNAMETxt.Text = ""
                        REMARKTxt.Text = ""
                        pckproductStatusChk.Checked = False
                    End If
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน")
                End If
            Else
                MsgBox(PROVIDER_Check.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub
End Class